package com.wentao.thisService.service.impl;

import com.wentao.thisCommon.model.RequestBase;
import com.wentao.thisFacade.common.enums.ExceptionCode;
import com.wentao.thisFacade.common.enums.business.HotelReviewStatusEnum;
import com.wentao.thisFacade.common.enums.business.OrderFormCode;
import com.wentao.thisFacade.common.enums.business.ValidStatusEnum;
import com.wentao.thisFacade.common.exception.CustomException;
import com.wentao.thisFacade.domain.reqeust.SearchSettingListRequest;
import com.wentao.thisFacade.domain.result.CompanyAddressInfoResult;
import com.wentao.thisFacade.domain.result.SettingMapResult;
import com.wentao.thisService.common.TokenVerify;
import com.wentao.thisService.mapper.*;
import com.wentao.thisService.model.*;
import com.wentao.thisService.service.CommunalService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Auther: ZhaiYanTao
 * @Date: 2020/3/27 09:29
 * @Description: 通用service实现
 */
@Service
public class CommunalServiceImpl implements CommunalService {
    @Autowired
    private SettingMapDao settingMapDao;

    @Autowired
    private CompanyCustomDao companyCustomDao;

    @Autowired
    private ApproveDao approveDao;

    @Autowired
    private ContractDao contractDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public List<SettingMapResult> searchSettingList(RequestBase<SearchSettingListRequest> entity) throws Exception {
        SettingMapEntity searchSettingMap = new SettingMapEntity();
        searchSettingMap.setGroupTypeLIST(entity.getBody().getGroupTypeLIST());
        searchSettingMap.setGroupCodeLIST(entity.getBody().getGroupCodeLIST());

        List<SettingMapEntity> settingMapEntityList = settingMapDao.Search(searchSettingMap);

        List<SettingMapResult> settingMapResultList = new ArrayList<>();
        if (null != settingMapEntityList && settingMapEntityList.size() > 0) {
            for (SettingMapEntity settingMapEntity : settingMapEntityList) {
                SettingMapResult settingMapResult = transform(settingMapEntity);
                if (null != settingMapResult) {
                    settingMapResultList.add(settingMapResult);
                }
            }
        }

        return settingMapResultList;
    }

    /**
     * @see CommunalService#hotelReviewStatus(String)
     */
    @Override
    @TokenVerify
    public CompanyAddressInfoResult hotelReviewStatus(String companyCode) throws Exception {
        CompanyCustomEntity searchCompany = new CompanyCustomEntity();
        searchCompany.setCompanyCode(companyCode);
        List<CompanyCustomEntity> companyCustomEntityList = companyCustomDao.Search(searchCompany);
        if (null == companyCustomEntityList || companyCustomEntityList.size() == 0) {
            throw new CustomException(ExceptionCode.NOCOMPANY_ERROR);
        }

        CompanyCustomEntity companyCustom = companyCustomEntityList.get(0);
        String approveStep = companyCustom.getApproveStep();
        String approveStatus = companyCustom.getApproveStatus();

        // 签订协议的状态
        ContractEntity contractEntity = new ContractEntity();
        contractEntity.setCompanyCustomId(companyCustom.getId());
        List<ContractEntity> contractEntities = contractDao.Search(contractEntity);

        // 订单信息
        OrderEntity searchOrder = new OrderEntity();
        searchOrder.setUserId(companyCustom.getUserId());
        List<OrderEntity> orderEntityList = orderDao.Search(searchOrder);

        String hotelReviewStatus = "00";
        String hotelReviewOpinion = null;
        if ("W".equals(approveStep) && null == companyCustom.getAddress()) {
            // 表示要跳转到酒店信息页面
            hotelReviewStatus = "11";
        } else if ("W".equals(approveStep) && null != companyCustom.getAddress()) {
            // 表示要跳转到酒店资质页面
            hotelReviewStatus = "12";
        } else if ("U".equals(approveStep) && ("U".equals(approveStatus) || "Y".equals(approveStatus))) {
            // 表示要跳转到审核中页面
            hotelReviewStatus = "20";
        } else if ("U".equals(approveStep) && ("S".equals(approveStatus))) {
            if (null != contractEntities && contractEntities.size() > 0) {
                if (null != orderEntityList && orderEntityList.size() > 0) {
                    // 是否已支付订单
                    boolean havePaid = false;
                    List<String> orderPaidCodeList = Arrays.asList(OrderFormCode.COMPLETION_OF_PANYMENT.getCode(), OrderFormCode.THE_DELIVERY_COMPLETED.getCode(), OrderFormCode.ORDER_CPMPLETION.getCode());
                    for (OrderEntity orderEntity : orderEntityList) {
                        if (null != orderEntity && orderPaidCodeList.contains(orderEntity.getOrderStatus())) {
                            havePaid = true;
                            break;
                        }
                    }
                    if (havePaid) {
                        // 表示采购订单完成，要跳转到入驻成功页面
                        hotelReviewStatus = "31";
                    } else {
                        // 表示采购订单未完成
                        hotelReviewStatus = "30";
                    }
                } else {
                    // 表示要跳转到已签署协议,采购酒水页面
                    hotelReviewStatus = "23";
                }
            } else {
                // 表示要跳转到通过审核，待签署协议页面
                hotelReviewStatus = "21";
            }
        } else if ("U".equals(approveStep) && ("F".equals(approveStatus))) {
            // 表示要跳转到审核驳回页面
            hotelReviewStatus = "22";
            ApproveEntity searchApprove = new ApproveEntity();
            searchApprove.setCode(companyCustom.getId().toString());
            searchApprove.setApproveStatus("F");
            searchApprove.setStatus("Y");
            List<ApproveEntity> approveEntityList = approveDao.Search(searchApprove);
            if (null != approveEntityList && approveEntityList.size() > 0) {
                hotelReviewOpinion = approveEntityList.get(0).getRemark();
            }
        }

        CompanyAddressInfoResult result = new CompanyAddressInfoResult();
        result.setCompanyCode(companyCode);
        result.setRemark(hotelReviewStatus);
        result.setReason(hotelReviewOpinion);

        return result;
    }

    /**
     * @see CommunalService#hotelReviewStatus(String)
     */
    @Override
    public Map<String, String> hotelReviewStatus() throws Exception {
        // 签订协议的状态
        List<ContractEntity> signedCompanyCustomList = contractDao.SearchContractSignedCompanyCustomId();
        Set<Long> signedCompanyCustomIdSet = new HashSet<>();
        if (null != signedCompanyCustomList && signedCompanyCustomList.size() > 0) {
            for (ContractEntity contractEntity : signedCompanyCustomList) {
                if (null != contractEntity) {
                    signedCompanyCustomIdSet.add(contractEntity.getCompanyCustomId());
                }
            }
        }
        List<Long> signedCompanyCustomIdList = new ArrayList<>(signedCompanyCustomIdSet);

        // 订单信息
        List<OrderEntity> searchBuyingTaskCompletedUserList = orderDao.SearchBuyingTaskCompletedUserId();
        Set<Long> taskCompletedUserIdSet = new HashSet<>();
        if (null != searchBuyingTaskCompletedUserList && searchBuyingTaskCompletedUserList.size() > 0) {
            for (OrderEntity orderEntity : searchBuyingTaskCompletedUserList) {
                if (null != orderEntity && orderEntity.getDiscount() > 100000) {
                    taskCompletedUserIdSet.add(orderEntity.getUserId());
                }
            }
        }
        List<Long> taskCompletedUserIdList = new ArrayList<>(taskCompletedUserIdSet);

        List<OrderEntity> buyingTaskNotCompletedUserList = orderDao.SearchBuyingTaskNotCompletedUserId();
        Set<Long> taskNotCompletedUserIdSet = new HashSet<>();
        if (null != buyingTaskNotCompletedUserList && buyingTaskNotCompletedUserList.size() > 0) {
            for (OrderEntity orderEntity : buyingTaskNotCompletedUserList) {
                if (null != orderEntity && orderEntity.getDiscount() > 100000) {
                    taskNotCompletedUserIdSet.add(orderEntity.getUserId());
                }
            }
        }
        List<Long> taskNotCompletedUserIdList = new ArrayList<>(taskNotCompletedUserIdSet);


        // 酒店信息
        CompanyCustomEntity searchCompany = new CompanyCustomEntity();
        searchCompany.setStatus(ValidStatusEnum.VALID.getCode());
        List<CompanyCustomEntity> companyCustomEntityList = companyCustomDao.Search(searchCompany);

        Map<String, String> companyCodeReviewStatusMap = new HashMap<>();
        if (null != companyCustomEntityList && companyCustomEntityList.size() > 0) {
            String hotelReviewStatus = HotelReviewStatusEnum.OTHER.getCode();
            for (CompanyCustomEntity companyCustomEntity : companyCustomEntityList) {
                if (null != companyCustomEntity) {
                    String approveStep = companyCustomEntity.getApproveStep();
                    String approveStatus = companyCustomEntity.getApproveStatus();
                    String address = companyCustomEntity.getAddress();
                    if ("W".equals(approveStep) && StringUtils.isNotBlank(address)) {
                        // 表示要跳转到酒店信息页面
                        hotelReviewStatus = HotelReviewStatusEnum.ADD_INFO.getCode();
                    } else if ("W".equals(approveStep) && StringUtils.isNotBlank(address)) {
                        // 表示要跳转到酒店资质页面
                        hotelReviewStatus = HotelReviewStatusEnum.ADD_LICENSE.getCode();
                    } else if ("U".equals(approveStep) && ("U".equals(approveStatus) || "Y".equals(approveStatus))) {
                        // 表示要跳转到审核中页面
                        hotelReviewStatus = HotelReviewStatusEnum.IN_REVIEW.getCode();
                    } else if ("U".equals(approveStep) && ("S".equals(approveStatus))) {
                        if (signedCompanyCustomIdList.contains(companyCustomEntity.getId())) {
                            if (taskCompletedUserIdList.contains(companyCustomEntity.getUserId())) {
                                // 表示采购订单完成，要跳转到入驻成功页面
                                hotelReviewStatus = HotelReviewStatusEnum.BUYING_TASK_COMPLETED.getCode();
                            } else if (taskNotCompletedUserIdList.contains(companyCustomEntity.getUserId())) {
                                // 表示采购订单未完成
                                hotelReviewStatus = HotelReviewStatusEnum.BUYING_TASK.getCode();
                            } else {
                                // 表示要跳转到已签署协议,采购酒水页面
                                hotelReviewStatus = HotelReviewStatusEnum.CONTRACT_COMPLETED.getCode();
                            }
                        } else {
                            // 表示要跳转到通过审核，待签署协议页面
                            hotelReviewStatus = HotelReviewStatusEnum.REVIEW_ACCEPT.getCode();
                        }
                    } else if ("U".equals(approveStep) && ("F".equals(approveStatus))) {
                        // 表示要跳转到审核驳回页面
                        hotelReviewStatus = HotelReviewStatusEnum.REVIEW_REFUSE.getCode();
                    }
                    companyCodeReviewStatusMap.put(companyCustomEntity.getCompanyCode(), hotelReviewStatus);
                }
            }
        }
        return companyCodeReviewStatusMap;
    }

    /**
     * 转换方法
     *
     * @param source PO
     * @return VO
     */
    private SettingMapResult transform(SettingMapEntity source) {
        if (null != source) {
            SettingMapResult target = new SettingMapResult();

            target.setId(source.getId());
            target.setGroupCode(source.getGroupCode());
            target.setGroupType(source.getGroupType());
            target.setKeyCode(source.getKeyCode());
            target.setValueCode(source.getValueCode());
            target.setStatus(source.getStatus());

            return target;
        }
        return null;
    }
}
